package com.liutt.shopmanage.comtroller;


import com.liutt.shopcommon.api.CommonPage;
import com.liutt.shopcommon.api.CommonResult;
import com.liutt.shopmanage.mbg.model.PmsBrand;
import com.liutt.shopmanage.mbg.model.product.PmsBrandParam;
import com.liutt.shopmanage.service.PmsBrandSerVice;
import com.sun.org.slf4j.internal.Logger;
import com.sun.org.slf4j.internal.LoggerFactory;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 品牌 Unauthorized
 */
@Controller
@RequestMapping("/brand")
public class PmsBrandController {



    private static final Logger LOGGER = LoggerFactory.getLogger(PmsBrandController.class);


    @Autowired
    private PmsBrandSerVice pmsBrandService;

    /**
     * 获取所品牌列表
     *
     * @return
     */
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<PmsBrand>> getPmsBrandList() {
        return CommonResult.success(pmsBrandService.listAllBrand());

    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult createBrand(@Validated @RequestBody PmsBrandParam pmsBrand) {
        CommonResult commonResult;
        int count = pmsBrandService.createBrand(pmsBrand);
        if (count == 1) {
            commonResult = CommonResult.success(pmsBrand);
            LOGGER.debug("createBrand success:{}", pmsBrand);
        } else {
            commonResult = CommonResult.failed("操作失败");
            LOGGER.debug("createBrand failed:{}", pmsBrand);
        }
        return commonResult;
    }

    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateBrand(@PathVariable("id") Long id, @Validated @RequestBody PmsBrandParam pmsBrand) {
        CommonResult commonResult;

        int count = pmsBrandService.updateBrand(id, pmsBrand);
        if (count == 1) {
            commonResult = CommonResult.success(null);
        } else {
            commonResult = CommonResult.failed("操作失败");
        }
        return commonResult;
    }

    /**
     * 单个删除品牌
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult delete(@PathVariable("id") Long id) {
        CommonResult commonResult;
        int count = pmsBrandService.deleteBrand(id);
        if (count == 1) {
            commonResult = CommonResult.success(null);
        } else {
            commonResult = CommonResult.failed("删除失败");
        }
        return commonResult;
    }


    @ApiOperation(value = "批量删除品牌故事")
    @RequestMapping(value = "/delete/batch", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult deleteBrand(@RequestParam("ids") List<Long> ids) {
        CommonResult commonResult;
        int count = pmsBrandService.deleteBrand(ids);
        if (count == 1) {
            commonResult = CommonResult.success(null);
        } else {
            commonResult = CommonResult.failed("批量删除失败");
        }
        return commonResult;
    }


    @ApiOperation(value = "根据品牌名称分页删除")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult keyWorldList(@RequestParam(value = "keyword", required = false) String keyword,
                                     @RequestParam(value = "pageSize", defaultValue = "1") Integer pageSize,
                                     @RequestParam(value = "pageNum", defaultValue = "10") Integer pageNum) {
        List<PmsBrand> list = pmsBrandService.listBrand(keyword, pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(list));
    }

}
